import { Component, LogicRender } from 'refast';
import { withRouter } from 'react-router';
import SearchWord from 'components/search-word';
import SearchData from 'components/search-data';
import './PageTmc.less';
import logic from './logic';

window.maplet = null;

class PageTmc extends Component {

  constructor(props) {
    super(props, logic);
    this.state = {
      city: this.props.city || "杭州市"
    }
    this.handleChange = this.handleChange.bind(this);
    this.updateAndSearch = this.updateAndSearch.bind(this);
  }

  handleChange(e) {
    this.updateAndSearch({ workNo: e.target.value });
  }

  updateAndSearch(val) {
    this.execute(['updateState', 'search'], val);
  }

  render() {
    const { state: { data, workNo, empty, loading }, handleChange, props: { city } } = this;
    return (
      <div className="page-home" ref="page_tmc">
        <div id="mapbar"></div>
      </div>
    );
  }

  componentDidMount() {
    let width = window.innerWidth,
      height = window.innerHeight,
      t = this,
      { state: { city } } = t;
    let top = this.refs.page_tmc.offsetTop,
      left = this.refs.page_tmc.offsetLeft;
    maplet = new Maplet("mapbar");
    switch (city) {
      case '杭州市':
        maplet.centerAndZoom(new MPoint(120.18323, 30.31436), 10);
        break;
      case '嘉兴市':
        maplet.centerAndZoom(new MPoint(120.76488, 30.74098), 10);
        break;
      case '绍兴市':
        maplet.centerAndZoom(new MPoint(120.59973, 29.98929), 10);
        break;
      default:
        break;
    }
    maplet.addControl(new MStandardControl());
    maplet.resize(width - left - 40, height - top - 40);

  }

  componentWillReceiveProps(nextProps) {
    let t = this;
    if (t.state.city != nextProps.city) {
      t.setState({ city: nextProps.city });
      switch (nextProps.city) {
        case '杭州市':
          maplet.centerAndZoom(new MPoint(120.18323, 30.31436), 10);
          break;
        case '嘉兴市':
          maplet.centerAndZoom(new MPoint(120.76488, 30.74098), 10);
          break;
        case '绍兴市':
          maplet.centerAndZoom(new MPoint(120.59973, 29.98929), 10);
          break;
        default:
          break;
      }
    };

  }
}
export default withRouter(PageTmc);


